﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class bullet : MonoBehaviour {

    public int damage = 50;
    public float speed = 20;
    private Transform target;
    public GameObject ExplosionEffectPrefab;
    private float distanceArriveTarget = 1.2f;
    public void SetTarget(Transform _Target)
    {
        this.target = _Target;
    
    }
    void Update()
    {
        if (target == null)
        {
            Die();
            return;
        
        }
        transform.LookAt(target.position);
        transform.Translate(Vector3.forward * speed * Time.deltaTime);

        Vector3 dir = target.position - transform.position;
        if (dir.magnitude < distanceArriveTarget)
        {
            target.GetComponent<enemy>().TakeDamage(damage);
            Die();
        }
    }
    void Die()
    {
        GameObject effect = GameObject.Instantiate(ExplosionEffectPrefab, transform.position, transform.rotation);
        Destroy(effect, 1);
        Destroy(this.gameObject);
    
    }

}
